package com.TCP_UDP网络编程.TCP_Socket_网络编程;

import java.io.*;
import java.net.Socket;

@SuppressWarnings("all")
public class socketTCP02客户端_实现请求 {
    /** 注意  客户端和服务端各有Socket 用于传输*/
    public static void main(String[] args) throws IOException {
        System.out.println("客户端尝试连接~~~~");
        /// 1.连接IP为10.110.226.177主机的9999端口；跟9999端口形成通道；
        Socket socket = new Socket("10.110.226.177",9999);
        /// 2.通过socket.getOutputStream() 得到一个输出节点流  给服务器发送hello world
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("hello 服务端".getBytes());
        /// 3.发送hello 服务端 后关闭，防止对方一直读取导致阻塞；
        socket.shutdownOutput();

        /// 4.读取 服务端返回的 hello 客户端；
        InputStream inputStream = socket.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        String readLine = "";
        while((readLine = br.readLine()) != null){
            System.out.println(readLine);
        }


        /// 5.关闭节点流和socket
        br.close();
        outputStream.close();
        socket.close();
    }
}
